package daoBeans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.SynchronizationType;

import entidades.Emprestimo;

/**
 * Session Bean implementation class EmprestimoDAO
 * @author Eli Moura
 */
@Stateless
public class EmprestimoDAO implements EmprestimoDAORemote {
	
	@PersistenceContext(unitName="Biblioteca", synchronization=SynchronizationType.SYNCHRONIZED)
    private EntityManager em;
	
    /**
     * Default constructor. 
     */
    public EmprestimoDAO() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public Emprestimo findEmprestimoById(int id) {
		Query query = em.createNamedQuery("findEmprestimoById");
        query.setParameter("id", id);
        Emprestimo emprestimo = (Emprestimo) query.getSingleResult();
        return emprestimo;
	}

	@Override
	public Emprestimo findEmprestimoByUsuario(String matricula) {
		Query query = em.createNamedQuery("findEmprestimoByUsuario");
        query.setParameter("nome", matricula);
        Emprestimo emprestimo = (Emprestimo) query.getSingleResult();
        return emprestimo;
	}

	@Override
	public void newEmprestimo(Emprestimo emprestimo) {
		em.persist(emprestimo);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Emprestimo> findEmprestimos() {
		Query query = em.createNamedQuery("findEmprestimos");
        return query.getResultList();
	}

}
